/*******************************************************************************
File: 1_baseline_social_capital
Project: CPKIN
Purpose: This .do file prepares baseline outcome for subgroup analysis
Author: Akshay Dixit
Date Created: Mar 7, 2019
Date last modified: June 28, 2020
********************************************************************************/

clear all
set more off

***THIS IS THE INPUT THAT MUST BE CHANGED SPECIFIC TO THE USER***
*gl identity ""
*gl user "" 
		   
********************************************************************************

***Creating Global File Paths***'
gl data "$user/data"
gl dof "$user/do files"
gl analysis "$user/analysis"

********************************************************************************

***Separating the FP dataset from the others***

cd "$user"

u "$data/baseline_master.dta", clear

drop if type_survey == "child"	//Dropping child survey - these questions were not included in the child survey
drop if type_survey == "focal person"

********************************************************************************

***Social capital***

ren neibours_property neighbours_property
desc com_property believe_people share_water separate_fight neighbours_food neighbours_property /*
*/ visit_neighbours_home visit_neighbours_street do_together safe_from_crime

	// Create binary variables that equal 1 when the response is "Yes, always"
foreach var in com_property believe_people share_water separate_fight neighbours_food neighbours_property {
	tab `var'
	g always_`var' = (`var' == "3 Yes Always")
	replace always_`var' = . if `var' == ""
	replace always_`var' = . if `var' == "8888 Don't Know"
	replace always_`var' = . if `var' == "9999 No Response"
	tab always_`var'
}

	// Only a handful of missing values, and not concentrated in any particular village
foreach var in com_property believe_people share_water separate_fight neighbours_food neighbours_property {
	di "always_`var'"
	count if always_`var' == .
	tab village_id1 if always_`var' == .
}

	// Binary variable that equals 1 when response is "Often"
g often_visit_neighbours_home = (visit_neighbours_home == "3 Often")
replace often_visit_neighbours_home = . if visit_neighbours_home == ""		//Four missing values

g often_visit_neighbours_street = (visit_neighbours_street == "3 Often")
replace often_visit_neighbours_street = . if visit_neighbours_street == ""	//Four missing values

g often_do_together	= (do_together == "Often")
replace often_do_together = . if do_together == ""							//Seven missing values

	//Binary equals 1 when response is "Very safe"
g very_safe = (safe_from_crime == "3 Very Safe")
replace very_safe = . if safe_from_crime == ""

	//Index of social capital - Sum of 10 components 
egen social_capital = rowtotal(always_com_property always_believe_people always_share_water always_separate_fight ///
always_neighbours_food always_neighbours_property often_visit_neighbours_home often_visit_neighbours_street ///
often_do_together very_safe), missing


* Collapse by village ID
collapse (mean) social_capital, by(village_id1)

* Add "bl_" prefix to variable names
foreach x of varlist _all {
	ren `x' bl_`x'
	lab var bl_`x' "Baseline"
}
ren bl_village_id1 village_id1
lab var village_id1 "Village ID"

save "$data/baseline_social_capital.dta", replace

clear





